#!/sbin/openrc-run

name="redict server"

: ${cfgfile:=${REDICT_CONF:-"/etc/redict.conf"}}
: ${command_user:="${REDICT_USER:-redict}:${REDICT_GROUP:-redict}"}
: ${retry:=30}

command="/usr/bin/redict-server"
command_args="$cfgfile --daemonize no $command_args"
command_background="yes"
pidfile="/run/$RC_SVCNAME.pid"

required_files="$cfgfile"

depend() {
	use net localmount logger
	after keepalived firewall
}

start_pre() {
	# Sets start-start-daemon's --chdir.
	directory=$(config_get 'dir' '/var/lib/redict')
	checkpath -d -o "$command_user" "$directory"

	local logfile=$(config_get 'logfile')
	[ "$logfile" ] && checkpath -d -o "$command_user" "${logfile%/*}"

	local unixsocket=$(config_get 'unixsocket')
	[ "$unixsocket" ] && checkpath -d -o "$command_user" "${unixsocket%/*}"

	return 0
}

config_get() {
	local key="$1"
	local default="${2:-}"

	local value=$(awk "\$1 == \"$key\" { print \$2 }" "$cfgfile")
	printf '%s\n' "${value:-$default}"
}
